package util;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.huawei.eie.api.sm.DBSMProxy;
import com.huawei.eie.api.sm.SmReceiveBean;
import com.huawei.eie.api.sm.SmSendBean;
import com.huawei.eie.api.sm.SmSendResultBean;


public class CopyOfSmsSend {

	/**
	 *@author qingyu zhang
	 *@function:
	 * @param args
	 *2011-3-14
	 *下午05:04:06
	 *SmsTiaoyong
	 *
	 *void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*DBSMProxy smproxy = new SmsSend().init();
		if(smproxy!=null){
			try {
				//testSend(smproxy);
				//testQuerysmResult(smproxy);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("发送失败!");
			}finally{
				//退出登录。
			    smproxy.logout();
			    
			//销毁连接。
			    smproxy.destroy();

			}
		}*/
	}
	public String retureLogo(String DB_IP,String userName,String userPass,String MAS_Name,String MAS_Pass,String fromDate,String endDate,String orgAdd,String phone){
		System.out.println("进入！！！！！！！！！！！！！！！！！！！！！");
		DBSMProxy smproxy = new CopyOfSmsSend().init(DB_IP,userName,userPass,MAS_Name,MAS_Pass);
		String result ="";
		if(smproxy!=null){
			try {
				result = testQuerysmResult(smproxy,fromDate,endDate,orgAdd,phone);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		 smproxy.logout();
		 smproxy.destroy();
		return result;
	}
	//发送短信接口入口
	public void retureLogoSend(String DB_IP,String userName,String userPass,String MAS_Name,String MAS_Pass,String content,String orgAdd,String phone[],String serviceId,String sendDate){
		DBSMProxy smproxy = new CopyOfSmsSend().init(DB_IP,userName,userPass,MAS_Name,MAS_Pass);
		Date send_Date = Timestamp.valueOf(sendDate);
		String result ="";
		if(smproxy!=null){
			try {
				testSend(smproxy,phone,content,orgAdd,send_Date,serviceId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//初始化数据库的连接
	public DBSMProxy init(String DB_IP,String userName,String userPass,String MAS_Name,String MAS_Pass){
		boolean bo = false;
		DBSMProxy smproxy = new DBSMProxy();
	    Map paras = new HashMap();
	    //MAS短信接口DB的URL。
		paras.put("url","jdbc:microsoft:sqlserver://"+DB_IP+":1433;DatabaseName=DB_CustomSMS");
			//MAS短信接口db登录用户名。
		paras.put("user",userName);
		//MAS短信接口db登录的密码。
		paras.put("password",userPass);
		//以下示例提供了3种初始化连接的方法。
	    try {
			smproxy.initConn(paras);
			//登录MASmas短信应用接口，业务登录用户名、业务登录密码。
		    smproxy.login(MAS_Name, MAS_Pass);
			bo = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//通过上面设定的参数初始化。
		//	    smproxy.initConn();//自动查找配置文件的方式初始化，参见上面函数说明。
			//手动查找配置文件的方式，参见上面函数说明。
		//	    smproxy.initConn("./apiconf/smapiconf.xml");
		return smproxy;

	}
	//发送短信
	 public static final void testSend(DBSMProxy smproxy,String phone[],String content,String orgAdd,Date sendDate,String serviceId)
     throws Exception
     {
		 SmSendBean bean = new SmSendBean();
		 String phones[] = phone;//new String[]{"15854132785"};
		 bean.setSmDestAddrs(phones);
		 bean.setSmMsgContent(content);
		 bean.setSmNeedStateReport(1);
		 bean.setSmOrgAddr(orgAdd);
		 bean.setSmSendTime(sendDate);
		 bean.setSmServiceId(serviceId);
		 int [] result = smproxy.sendSm(bean);
		 System.out.println("result=="+result);
     }
	 //对发送状态查询
	 public static final String testQuerysmResult(DBSMProxy smproxy,String fromDate,String endDate,String orgAdd,String phone)
     throws Exception
     {
		 //构造查询条件，入口参数详细说明参见上面函数说明。
		 Date startDate= Timestamp.valueOf(fromDate);
		 Date end_Date= Timestamp.valueOf(endDate);
		 System.out.println("startDate=="+startDate+"end_Date=="+end_Date);
		 String result= "";
		    SmSendResultBean[] beans =
		        smproxy.querySmsResult(-1
		        , startDate
		        , end_Date
		        ,orgAdd,phone);
		    for (SmSendResultBean bean:beans)
		    {
		    	result+=bean.getSmMsgContent()+"/"+bean.getSmRecvStatus()+",";
		      System.out.println("bean:"+bean.getSmMsgContent()+"接收状态"+bean.getSmRecvStatus());
		    }
		    return result;

     }

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-3-14
	 *下午05:04:06
	 *SmsTiaoyong
	 *
	 */
}
